<?php
require_once 'controller.php';
require_once 'user_model.php';


/**
 * user_controller Class
 * 
 * Controller for user actions
 * 
 * @author Victor Nava
 *
 */
class user_controller extends controller{
	public $request;
	public $userModel;
	public $view;

	/**
	 * Constructor
	 */
	function __construct(){
		$this->userModel = new user_model();
	}

	/**
	 * Default action, redirect to user page is user is logged in
	 * NOT IMPLEMENTED
	 * @return void
	 */
	function index(){
		$this->requireLogin();
		$view->user = $_SESSION['user'];
		include("user_index_view.php");
	}

	/**
	 * View user page action
	 * NOT IMPLEMENTED
	 * @return void
	 */
	function view(){
		$this->requireLogin();
		include("user_view.php");
	}

	/**
	 * Login action
	 * Try to login a user and redirect to previous page
	 * @return void
	 */
	function login(){
		if ($_SERVER['REQUEST_METHOD'] == 'POST'){
			if( !isset($this->request['email']) or !isset($this->request['password'])){
				$this->setFlash("Email and Password arguments must be passed.");
				include('user_login_view.php');
				exit();
			}

			$email = $this->request['email'];
			$pass = $this->request['password'];
			$user = $this->userModel->getByEmailPass($email, $pass);

			$this->_login($email, $pass);
			//redirect(HOME_PAGE);
			redirect($_SESSION['beforeLogin']);
		}
		else{
			//$_SESSION['beforeLogin'] = $_SESSION['referer'];
			$_SESSION['beforeLogin'] = $_SESSION['referer'];
			include('user_login_view.php');
		}
	}

	/**
	 * Login a user
	 * 
	 * @param $email
	 * @param $pass
	 * @return unknown_type
	 */
	private function _login($email, $pass){
		$user = $this->userModel->getByEmailPass($email, $pass);

		if(!$user){
			//$view->flash = "Invalid email or password, please try again.";
			$this->setFlash("Invalid email or password, please try again.");
			include('user_login_view.php');
			exit();
		}
		$_SESSION['user'] = $user;
	}
	
	/**
	 * Register action
	 * @return unknown_type
	 */
	function register(){

		$minLength = 4;
		$maxLength = 32;

		if ($_SERVER['REQUEST_METHOD'] == 'POST'){

			if( !isset($this->request['email']) or !isset($this->request['password']) or !isset($this->request['name'])){
				$this->setFlash( "Name, Email and Password arguments must be passed");
				include('user_register_view.php');
				exit();
			}

			//TODO sanitize
			$name = $this->request['name'];
			$email = $this->request['email'];
			
			//TODO md5 password
			$pass = $this->request['password'];

			if( !Validate::string($name, array('min_length' => 1))){
				$this->setFlash( "Hey you forgot your name! please try again.");
				include('user_register_view.php');
				exit();
			}

			if( !Validate::email($email)){
				$this->setFlash( "Your email address looks funny, please make sure is correct.");
				include('user_register_view.php');
				exit();
			}

			if( !Validate::string($pass, array('min_length' => $minLength, 'max_length' =>32))){
				$this->setFlash( "Your password must be between $minLength and $maxLength characters long.");
				include('user_register_view.php');
				exit();
			}

			$user = $this->userModel->getByEmailPass($email, $pass);

			if($user){
				$this->setFlash( "Looks like you are already registered, try to login instead.");
				include('user_register_view.php');
				exit();
			}

			//TODO md5 password
			$user = $this->userModel->create($this->request);

			if(!$user){
				$this->setFlash( "Upps, there was a problem with your registration please try again.");
				include('user_register_view.php');
				exit();
			}

			$this->_login($email, $pass);
			redirect(HOME_PAGE);
		}
		else{
			include('user_register_view.php');
		}
	}

	/**
	 * Logout action 
	 * @return void
	 */
	function logout(){
		$_SESSION = array ();
		session_destroy();
		redirect(HOME_PAGE);
	}

	/************* test *************/

	/**
	 * test
	 * @return unknown_type
	 */
	function getTestUser(){
		return array("id" => 0, "name" => "vic", "email"=> "vic@tor.com");
	}
	
	/**
	 * test
	 * @return unknown_type
	 */
	function getUser(){
		if( !isset($this->request['email']) or !isset($this->request['password'])){
			echo 'email and pass must be passed';
			exit();
		}
		$email = $this->request['email'];
		$pass = $this->request['password'];
		return $this->userModel->getByEmailPass($email, $pass);
	}

	/**
	 * test
	 * @return unknown_type
	 */
	function viewAll(){
		print_r($this->userModel->getAll());
	}
}
?>